import 'dart:convert';
import 'dart:io';
import '../http/request_api.dart';
import '../model/base_response.dart';
import 'base_api_service.dart';

class PaymentService extends BaseApiService {
  static final PaymentService _instance = PaymentService._internal();

  factory PaymentService() {
    return _instance;
  }

  PaymentService._internal();

  /// 创建订单（下单接口）
  Future<BaseResponse<Map<String, dynamic>>> createOrder({
    required String productId,
    required int contarctId,
  }) async {
    final data = {
      "goods_id": contarctId,
    };

    return await post<Map<String, dynamic>>(
      RequestApi.CREATE_ORDER,
      data: data,
      showLoading: true,
      fromJsonT: (data) => data as Map<String, dynamic>,
    );
  }

  /// 验证支付（验证接口）
  Future<BaseResponse<Map<String, dynamic>>> verifyPayment({
    required String orderId,
    required String purchaseData,
  }) async {
  
    if (Platform.isIOS) {
      final String platform = 'apple_store';
      final data = {
      "oid": orderId,
      "receipt_data": purchaseData,
      "platform": platform,
    };

    return await post<Map<String, dynamic>>(
      RequestApi.VERIFY_APPLE_PAYMENT,
      data: data,
      showLoading: true,
      fromJsonT: (data) => data as Map<String, dynamic>,
    );
    } else {
      final String platform = 'google_play';
         final data = {
      "oid": orderId,
      "purchase_data": purchaseData,
      "platform": platform,
    };

    return await post<Map<String, dynamic>>(
      RequestApi.VERIFY_PAYMENT,
      data: data,
      showLoading: true,
      fromJsonT: (data) => data as Map<String, dynamic>,
    );
    }

 
  }
}
